package com.enqualcomm.kids.activities;

import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;

import com.android.volley.VolleyError;
import com.enqualcomm.kids.R;
import com.enqualcomm.kids.base.BaseActivity;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.config.userdefault.TerminalDefault;
import com.enqualcomm.kids.config.userdefault.UserTerminalDefault;
import com.enqualcomm.kids.mvp.NetworkModel;
import com.enqualcomm.kids.network.NetworkListener;
import com.enqualcomm.kids.network.SocketRequest;
import com.enqualcomm.kids.network.socket.request.SetWatchVolumeParams;
import com.enqualcomm.kids.network.socket.request.TerminalConfigParams;
import com.enqualcomm.kids.network.socket.response.BasicResult;
import com.enqualcomm.kids.network.socket.response.QueryUserTerminalInfoResult;
import com.enqualcomm.kids.network.socket.response.TerminalConfigResult;
import com.enqualcomm.kids.network.socket.response.TerminallistResult;

import java.util.Locale;

import common.utils.PromptUtil;

public class WatchVolumeActivity extends BaseActivity implements SeekBar.OnSeekBarChangeListener {

    private SeekBar mRingProgressBar;
    private SeekBar mCallProgressBar;

    private int mRing_Volume;
    private int mCall_Volume;
    private TerminallistResult.Terminal terminal;
    private TerminalConfigResult.Data config;
    private TerminalDefault terminalDefault;
    private AppDefault appDefault;
    private NetworkModel networkModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_watch_volume);

        terminal = getTerminal();
        appDefault = new AppDefault();
        terminalDefault = new TerminalDefault(terminal.terminalid);
        config = terminalDefault.getConfig();
        mRing_Volume = config.ring_vol;
        mCall_Volume = config.call_vol;

        networkModel = new NetworkModel();
        initView();
        syncConfig();
    }

    private void syncConfig() {
        //逗比需求
        networkModel.loadDataFromServer(new SocketRequest<TerminalConfigResult>(new TerminalConfigParams(appDefault.getUserkey(), terminal.terminalid),
                new NetworkListener<TerminalConfigResult>() {
                    @Override
                    public void onError(VolleyError error) {
                    }

                    @Override
                    public void onSuccess(TerminalConfigResult response) {
                        if (response.code == 0) {
                            config.ring_vol = mRing_Volume = response.result.ring_vol;
                            config.call_vol = mCall_Volume = response.result.call_vol;
                            terminalDefault.setConfig(config);
                            //刷新进度条

                            updateProgressView();
                        }
                    }
                }));
    }

    private void initView() {
        findViewById(R.id.title_bar_left_iv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        QueryUserTerminalInfoResult.Data info = new UserTerminalDefault(terminal.userterminalid).getInfo();
        if ("ZH".equals(Locale.getDefault().getLanguage().toUpperCase(Locale.getDefault()))) {
            setupTitleIcon(info,terminal.terminalid, terminal.userterminalid, getString(R.string.watch_volume_title));
        }else {
            setupTitleIcon(info, terminal.terminalid, terminal.userterminalid, "Back");
        }

        mRingProgressBar = (SeekBar) findViewById(R.id.ring_progress);
        mCallProgressBar = (SeekBar) findViewById(R.id.call_progress);

        mRingProgressBar.setMax(100);
        mCallProgressBar.setMax(100);

        updateProgressView();

        mRingProgressBar.setOnSeekBarChangeListener(this);
        mCallProgressBar.setOnSeekBarChangeListener(this);
    }

    private void updateProgressView(){
        if (mRing_Volume == 0) {
            mRingProgressBar.setProgress(0);
        } else if (mRing_Volume == 1) {
            mRingProgressBar.setProgress(50);
        } else {
            mRingProgressBar.setProgress(100);
        }

        if (mCall_Volume == 0) {
            mCallProgressBar.setProgress(0);
        } else if (mCall_Volume == 1) {
            mCallProgressBar.setProgress(50);
        } else {
            mCallProgressBar.setProgress(100);
        }
    }

    private int mRingStartProgress;
    private int mCallStartProgress;

    @Override
    public void onProgressChanged(SeekBar seekBar, int changeProgress, boolean b) {
    }


    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
//        switch (seekBar.getId()) {
//            case R.id.ring_progress:
//                mRingStartProgress = seekBar.getProgress();
//                break;
//            case R.id.call_progress:
//                mCallStartProgress = seekBar.getProgress();
//                break;
//        }
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
//        int endProgress = seekBar.getProgress();
//        MyLogger.jLog().i(mRingStartProgress);
//        MyLogger.jLog().i(endProgress);
//        switch (seekBar.getId()) {
//            case R.id.ring_progress:
//                if (mRing_Volume == 2 && (mRingStartProgress - endProgress > 60)) {
//                    mRing_Volume = 0;
//                    mRingProgressBar.setProgress(0);
//                } else if (mRing_Volume == 2 && (mRingStartProgress - endProgress <= 5)) {
//                    mRing_Volume = 2;
//                    mRingProgressBar.setProgress(100);
//                } else if (mRing_Volume == 2 && (mRingStartProgress - endProgress > 5)) {
//                    mRing_Volume = 1;
//                    mRingProgressBar.setProgress(50);
//                } else if (mRing_Volume == 1 && (endProgress - mRingStartProgress <= 5) && (mRingStartProgress - endProgress <= 5)) {
//                    mRing_Volume = 1;
//                    mRingProgressBar.setProgress(50);
//                } else if (mRing_Volume == 1 && (mRingStartProgress - endProgress > 5)) {
//                    mRing_Volume = 0;
//                    mRingProgressBar.setProgress(0);
//                } else if (mRing_Volume == 1 && (endProgress - mRingStartProgress > 5)) {
//                    mRing_Volume = 2;
//                    mRingProgressBar.setProgress(100);
//                } else if (mRing_Volume == 0 && (endProgress - mRingStartProgress > 60)) {
//                    mRing_Volume = 2;
//                    mRingProgressBar.setProgress(100);
//                } else if (mRing_Volume == 0 && (endProgress - mRingStartProgress <= 5)) {
//                    mRing_Volume = 0;
//                    mRingProgressBar.setProgress(0);
//                } else if (mRing_Volume == 0 && (endProgress - mRingStartProgress <= 50)) {
//                    mRing_Volume = 1;
//                    mRingProgressBar.setProgress(50);
//                }
//                break;
//            case R.id.call_progress:
//                if (mCall_Volume == 2 && (mCallStartProgress - endProgress > 60)) {
//                    mCall_Volume = 0;
//                    mCallProgressBar.setProgress(0);
//                } else if (mCall_Volume == 2 && (mCallStartProgress - endProgress <= 5)) {
//                    mCall_Volume = 2;
//                    mCallProgressBar.setProgress(100);
//                } else if (mCall_Volume == 2 && (mCallStartProgress - endProgress > 5)) {
//                    mCall_Volume = 1;
//                    mCallProgressBar.setProgress(50);
//                } else if (mCall_Volume == 1 && (endProgress - mCallStartProgress <= 5) && (mCallStartProgress - endProgress <= 5)) {
//                    mCall_Volume = 1;
//                    mCallProgressBar.setProgress(50);
//                } else if (mCall_Volume == 1 && (mCallStartProgress - endProgress > 5)) {
//                    mCall_Volume = 0;
//                    mCallProgressBar.setProgress(0);
//                } else if (mCall_Volume == 1 && (endProgress - mCallStartProgress > 5)) {
//                    mCall_Volume = 2;
//                    mCallProgressBar.setProgress(100);
//                } else if (mCall_Volume == 0 && (endProgress - mCallStartProgress > 60)) {
//                    mCall_Volume = 2;
//                    mCallProgressBar.setProgress(100);
//                } else if (mCall_Volume == 0 && (endProgress - mCallStartProgress <= 5)) {
//                    mCall_Volume = 0;
//                    mCallProgressBar.setProgress(0);
//                } else if (mCall_Volume == 0 && (endProgress - mCallStartProgress <= 50)) {
//                    mCall_Volume = 1;
//                    mCallProgressBar.setProgress(50);
//                }
//                break;
//        }

        int progress = seekBar.getProgress();
        switch (seekBar.getId()){
            case R.id.ring_progress:
                if (progress<25){
                    mRing_Volume = 0;
                    mRingProgressBar.setProgress(0);
                }else if(progress<75){
                    mRing_Volume = 1;
                    mRingProgressBar.setProgress(50);
                }else{
                    mRing_Volume = 2;
                    mRingProgressBar.setProgress(100);
                }
                break;
            case R.id.call_progress:
                if (progress<25){
                    mCall_Volume = 0;
                    mCallProgressBar.setProgress(0);
                }else if(progress<75){
                    mCall_Volume = 1;
                    mCallProgressBar.setProgress(50);
                }else{
                    mCall_Volume = 2;
                    mCallProgressBar.setProgress(100);
                }
                break;
        }

        loadData(mRing_Volume, mCall_Volume);
    }

    private void loadData(final int ring_volume,final int call_volume) {
        showProgress();
        SetWatchVolumeParams params = new SetWatchVolumeParams(appDefault.getUserkey(),appDefault.getUserid()
                , terminal.terminalid, ring_volume, call_volume);
        networkModel.loadDataFromServer(new SocketRequest<BasicResult>(params, new NetworkListener<BasicResult>() {
            @Override
            public void onError(VolleyError error) {
                hideProgress();
                PromptUtil.toast(getApplicationContext(),R.string.app_no_connection);
            }

            @Override
            public void onSuccess(BasicResult response) {
                hideProgress();
                if (response.code==0){
                    config.ring_vol = ring_volume;
                    config.call_vol = call_volume;
                    terminalDefault.setConfig(config);
                    PromptUtil.toast(getApplicationContext(),getString(R.string.watch_volume_promote));
                }

            }
        }));
    }

    @Override
    protected void onStop() {
        super.onStop();
        networkModel.onStop();
    }
}
